// pages/list/index.js
import Toast from '../../ui/toast/index'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pic_url:app.pic_url,
        value: 0,
        options: [
            {
            value: 0,
            label: '全部产品',
            },
            
        ],
        sorter: {
            value: 0,
            options: [
                {
                value: 0,
                label: '默认排序',
                },
                {
                value: 1,
                label: '时间从高到低',
                },
                {
                    value: 2,
                    label: '时间从低到高',
                },
            ],
        },
        page:{
            page:1,
            cate_id:0,
            time:0,
        },
        list:[],
        loadingProps: {
            size: '50rpx',
        },
        loading:false,
    },
    //下拉
    onRefresh() {
        this.setData({ enable: true });
        setTimeout(() => {
          this.setData({ enable: false });
        }, 1000);
        this.get_list();
      },

    //分类切换
    onChange(e){
        let value = e.detail.value;
        this.data.page.cate_id = value;
        this.data.page.page=1;
        this.setData({
            value:value,
            list:[],
        });
        this.get_list();
    },

    //排序切换
    onChangeOrder(e){
        let value = e.detail.value;
        this.data.page.time = value;
        this.data.page.page=1;
        this.setData({
            list:[],
        });
        this.get_list();
    },

    //获取分类
    get_cate_list(){
        app.http.request("index.php?r=api/index/allcate").then((res)=>{
            if(res.data.result==1){
                let data = res.data.data;
                for(let i=0;i<data.length;i++){
                    let p={};
                    p.value = data[i].id;
                    p.label = data[i].str+data[i].cate_name;
                    this.data.options.push(p);
                }
                this.setData({
                    options:this.data.options,
                    value:this.data.page.cate_id,
                });
            }
        });
    },

    //获取内容
    get_list(){
        this.setData({
            loading:true,
        });
        app.http.request("index.php?r=api/index/goodslist",{page:this.data.page}).then((res)=>{
            this.setData({
                loading:false,
            });
            if(res.data.result==1){
                this.data.list = this.data.list.concat(res.data.data);
                this.setData({
                    list:this.data.list,
                });
                this.data.page.page++;
            }else{
                Toast({
                    selector:"#toast",
                    context:this,
                    message:res.data.data,
                });
            }
        });
    },

    //跳转内容
    to_content(e){
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/content/index?id='+id,
        })
    },

     //跳转
  to_url(e){
    let type = e.currentTarget.dataset.type;
    let url = "/pages/index/index";
    switch(type){
      case "cate":
        url = "/pages/cate/index";
        break;
      case "home":
        break;  
      case "news":
        url="/pages/news/list/index";
        break;
      case "us":
        url="/pages/us/index";
        break;    
    }
    wx.redirectTo({
      url: url,
    })
  },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let id = options.id;
        if(id){
            this.data.page.cate_id=id;
        }
        this.get_cate_list();
        this.get_list();
        wx.setNavigationBarTitle({
            title: wx.getStorageSync('web_name'),
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})